﻿using HtmlAgilityPack;

namespace MijnMultiOSRSSApp.Common.Helpers
{
    public class HtmlParser
    {
        public static string ExtractString(string htmlContent)
        {
            var innerHtml = htmlContent;
            if (innerHtml.IndexOfAny(new[] { '<', '>' }) >= 0)
            {
                var document = new HtmlDocument();
                document.LoadHtml(htmlContent);
                innerHtml = document.DocumentNode.InnerText;
            }
            return (HtmlEntity.DeEntitize(innerHtml) ?? "").Trim();
        }

        /// <summary>
        /// Remove unwanted markup from input string
        /// </summary>
        public static string CleanupText(string source)
        {
            // Cleanup start of content when needed
            if (source.StartsWith("\n"))
                source = source.Substring(1, source.Length - 1);
            return source
                .Replace("\\'", "'")
                .Replace("\r", " ")
                .Replace("\n", " ")
                .Replace("   ", " ")
                .Replace("  ", " ");
        }

        /// <summary>
        /// Cleanup HTML
        /// </summary>
        private static string CleanupHTML(string content)
        {
            // Interpret certain tags as new line characters
            content = content
                .Replace("\r", "\n")
                .Replace("\n<br />\n", "\n")
                .Replace("<br>", "\n")
                .Replace("<br />\n", "\n")
                .Replace("<br />", "\n")
                .Replace("</p>", "\n")
                .Replace("<p>", "\n")
                .Replace("&nbsp;", " ")
                .Replace("&euml;", "ë")
                .Replace("&iuml;", "ï")
                .Replace("&eacute;", "é")
                .Replace("&uuml;", "ü")
                .Replace("&amp;", "&")
                .Replace("&#39;", "'")
                .Replace("&#8211;", "-")
                .Replace("&#8216;", "‘")
                .Replace("&#8217;", "’")
                .Replace("&#8220;", "“")
                .Replace("&#8221;", "”")
                .Replace("&#8230;", "...");

            // Strip remaining HTML
            content = StripHTMLTags(content);

            // Cleaunup remaining content
            content = content
                .Replace("     ", " ")
                .Replace("    ", " ")
                .Replace("   ", " ")
                .Replace("  ", " ")
                .Replace("	", "")         // TAB
                .Replace(" \n", "\n")
                .Replace("\n\n\n\n\n\n\n", "\n\n")
                .Replace("\n\n\n\n\n\n", "\n\n")
                .Replace("\n\n\n\n\n", "\n\n")
                .Replace("\n\n\n\n", "\n\n")
                .Replace("\n\n\n", "\n\n")
                .Replace("\n ", "\n");

            // Cleanup start of content
            if (content.StartsWith("\n\n\n"))
                content = content.Substring(3, content.Length - 3);
            if (content.StartsWith("\n\n"))
                content = content.Substring(2, content.Length - 2);
            if (content.StartsWith("\n"))
                content = content.Substring(1, content.Length - 1);

            // Cleanup end of content
            if (content.EndsWith("\n\n\n"))
                content = content.Substring(0, content.Length - 3);
            if (content.EndsWith("\n\n"))
                content = content.Substring(0, content.Length - 2);
            if (content.EndsWith("\n"))
                content = content.Substring(0, content.Length - 1);
            return content;
        }
        
        /// <summary>
        /// Remove HTML tags from string using char array.
        /// </summary>
        public static string StripHTMLTags(string source)
        {
            if (source == null)
            {
                return "";
            }

            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }

    }
}
